import QtQuick 2.7
import QtQuick.Controls 1.4
import QtWebSockets 1.1
import QtQuick.Window 2.0
Window {
    id: window
    width: 500
    height: 500
    visible: true
        property var lastDisplay : 0
    Timer {
        interval: 500;
        running: true;
        repeat: true
        onTriggered: {
      if(!client.active){
        client.active = true
          }
        }
    }

    WebSocket{
	id: client
	url: "ws://127.0.0.1:11234"
	active:true
	onStatusChanged:{
		console.log(status)
		if(status==WebSocket.Closed){
		  client.active = false
		}
		//print(status)
	}
	onTextMessageReceived:{
//	  console.log(message)
	  var jsonM = JSON.parse(message);
	  if(jsonM.method==="face.updateUI"){
		if(lastDisplay + 50 < new Date().getTime()){
			img.source = "data:image/jpg;base64," + jsonM.payload.image
			lastDisplay = new Date().getTime()
		}
	  }else{
		console.log(message)
          }
	}
    }	    
    Image {
        id: img
        objectName: "img"
        //anchors.centerIn: parent
    anchors.fill: parent ;

    }

    Rectangle{
        color: Qt.rgba(0/255,0/255,0/255, 0.7)
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        height: 200
        Label{
            x: 0
            y: 0
            color: "white"
            text: "欢迎光临"
        }
        Button{
            x: 100
            y: 10
            text: "拍照"
        }
        Button{
            x: 100
            y: 60
            text: "人证匹配"
        }
        Button{
            x: 199
            y: 107
            text: "1:N"
        }
    }
}


